-- PinocchioFairy
-- Created by panyl Nov/10/2016
-- 匹诺曹仙女

-- 初始化
function initPinocchioFairy(self)
    self.item:removeAllChildren();
    local fairy_id = self.gridData.fairy_id;
    local quetion_id = self.gridData.quetion_id;

    if not fairy_id then
        catch(error);
        return;
    end

    local fairy_info = PinocchioM.query(fairy_id);

    -- 建筑图标
    local iconPath = getLevelItemIconPath(fairy_info["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            -- 如果不是仙女
            if GRID_TYPE_FAIRY ~= self.gridData.type or
                GRID_STATE_DISABLE == self.gridData.state then
                return;
            end

            -- TODO: 如果没有问题了，可以被雅典娜招募
            if not quetion_id or quetion_id <= 0 then
                local recruit = function()
                    DungeonActionM.go("recruit_fairy", self.gridData:getPos());
                end

                -- TODO: 偷懒
                local dialog = getLocStr("recruit_fairy_talk_3");
                if fairy_id == 1 then
                    dialog = getLocStr("recruit_fairy_talk_" .. math.random(1, 2));
                end
                local btnText = getLocStr("btn_text_go_to_fight");
                local name = PinocchioM.query(fairy_id, "name");

                self:openBuildingView(recruit, 0, dialog, btnText, name, iconPath);

                return;
            end

            local function yes()
                fairy_answer_yes(self);
            end
            local function no()
                fairy_answer_no(self);
            end

            -- 回答按钮
            local answer = PinocchioM.query_quetion(quetion_id, "answer");
            local say_hello = PinocchioM.get_say_hello(fairy_id, quetion_id);

            -- 如果需要连续回答，隐藏返回按钮
            local next_quetion = tonumber(PinocchioM.query_quetion(quetion_id, "next_quetion"));
            if next_quetion > 0 then
                self:openBuildingView2(yes, no, answer[1], answer[2], say_hello, iconPath, true, PinocchioM.query(fairy_id, "name"));
            else
                self:openBuildingView2(yes, no, answer[1], answer[2], say_hello, iconPath, false, PinocchioM.query(fairy_id, "name"));
            end
        end
    end
    self.icon:addTouchEventListener(onClicked);
end

-- 根据不同状态走不同剧本
-- 回答问题结果：1、正确  2、错误
--      正确要么播放效果结束，要么继续下一个问题
--      错误播放错误对白（或者没有对白），然后弹出提示
function play_fairy_scenario(self, state)
    local fairy_id = state["fairy_id"];
    local quetion_id = state["quetion_id"];
    local index = state["select"];

    local fairy_info = PinocchioM.query(fairy_id);

    -- 建筑图标
    local iconPath = getLevelItemIconPath(fairy_info["icon"]);

    if 1 == state.right then
        -- 正确结果

        -- 如果还有接下来的问题，继续问
        if self.gridData.quetion_id and self.gridData.quetion_id > 0 then
            -- 新的一轮问答
            local state = {
                ["fairy_id"] = fairy_id,
                ["quetion_id"] = self.gridData.quetion_id,
            };

            new_fairy_quetion(self, state);

            return;
        end

        local dialog = PinocchioM.query(fairy_id, "right_dialog");

        -- 没有对白，直接给提示
        if sizeof(dialog) <= 0 then
            -- 事件
            EventMgr.fire(event.ANSWER_FAIRY_QUETION, { ["pos"] = self.gridData:getPos(), ["right"] = state.right, ["fairy_id"] = fairy_id, ["type"] = self.gridData.type });

            alert(getLocStr("fairy_tip4"));
            -- 魅魔小屋特效
            playCabinEffect(self.node);

            performWithDelay(self, function() alert(getLocStr("fairy_tip5")); end, 2.8);
        else
            local function okFunc()
                -- 事件
                EventMgr.fire(event.ANSWER_FAIRY_QUETION, { ["pos"] = self.gridData:getPos(), ["right"] = state.right, ["fairy_id"] = fairy_id, ["type"] = self.gridData.type });

                -- 正确回答的提示
                local msgList = { getLocStr("fairy_tip1") };

                -- 如果鼻子缩短了
                if ME.user.noseToShort == true then
                    table.insert(msgList, getLocStr("fairy_tip2"))
                    -- 删掉标识
                    ME.user.noseToShort = nil;

                    -- 播放光效
                    playHeroAnswerEffect(state.right);
                end

                splitAlert(msgList);
            end

            local end_text = PinocchioM.query_quetion(quetion_id, "end")[1];
            self:openBuildingView(okFunc, 0, dialog, end_text, PinocchioM.query(fairy_id, "name"), iconPath, nil, true);
        end
    else
        -- 错误结果
        local text = PinocchioM.query(fairy_id, "wrong_dialog");

        -- 没有对白，直接给提示
        if sizeof(text) <= 0 then
            -- 事件
            EventMgr.fire(event.ANSWER_FAIRY_QUETION, { ["pos"] = self.gridData:getPos(), ["right"] = state.right, ["fairy_id"] = fairy_id, ["type"] = self.gridData.type });

            alert(getLocStr("fairy_tip6"))
        else
            local function okFunc()
                -- 事件
                EventMgr.fire(event.ANSWER_FAIRY_QUETION, { ["pos"] = self.gridData:getPos(), ["right"] = state.right, ["fairy_id"] = fairy_id, ["type"] = self.gridData.type });

                -- 错误回答的提示
                alert(getLocStr("fairy_tip3"));

                -- 播放光效
                playHeroAnswerEffect(state.right);
            end

            local wrong_text = PinocchioM.query_quetion(quetion_id, "wrong_text");
            local dialog = (wrong_text[index] or wrong_text[1] or "") .. text;
            local end_text = PinocchioM.query_quetion(quetion_id, "end")[2];
            self:openBuildingView(okFunc, 0, dialog, end_text, PinocchioM.query(fairy_id, "name"), iconPath, nil, true);
        end
    end
end

function new_fairy_quetion(self, para)
    local fairy_id = para["fairy_id"];
    local quetion_id = para["quetion_id"];
    local fairy_info = PinocchioM.query(fairy_id);

    -- 建筑图标
    local iconPath = getLevelItemIconPath(fairy_info["icon"]);

    -- 回答按钮
    local answer = PinocchioM.query_quetion(quetion_id, "answer");
    local dialog = PinocchioM.query_quetion(quetion_id, "desc");

    local function yes()
        fairy_answer_yes(self);
    end
    local function no()
        fairy_answer_no(self);
    end

    -- 弹出下一个问题界面
    -- 延时一下，防止和上个问题界面一起析构掉
    local function delayFunction()
        self:openBuildingView2(yes, no, answer[1], answer[2], dialog, iconPath, true, PinocchioM.query(fairy_id, "name"));
    end

    performWithDelay(self, delayFunction, 0.2);
end

-- 肯定回答
function fairy_answer_yes(self)
    local state = {
        ["fairy_id"]   = self.gridData.fairy_id,
        ["quetion_id"] = self.gridData.quetion_id,
        ["select"] = 1,
    };

    local ret, right = PinocchioM.try_answer(self.gridData:getPos(), 1);

    if not ret then
        return;
    end

    state["right"] = right;

    play_fairy_scenario(self, state);
end

-- 否定回答
function fairy_answer_no(self)
    local state = {
        ["fairy_id"]   = self.gridData.fairy_id,
        ["quetion_id"] = self.gridData.quetion_id,
        ["select"] = 2,
    };

    local ret, right = PinocchioM.try_answer(self.gridData:getPos(), 2);

    if not ret then
        return;
    end

    state["right"] = right;

    play_fairy_scenario(self, state);
end

function playHeroAnswerEffect(answer)
    local effectParent = UIDungeonMgr:getCurLevel():getEffectParent();
    local heroPos = UIDungeonMgr.getCurLevel():getHeroPos();


    answer = answer or 1;
    if 1 == answer then
        -- 奖励光效
        playEffect(effectParent, 1262, heroPos.x, heroPos.y - 85);
    else
        -- 惩罚光效
        playEffect(effectParent, 3024, heroPos.x, heroPos.y - 30);
    end
end
